﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: marsu
 * Date: 07/02/2015
 * Heure: 18:04
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;


using ComptaPerso.Controls;
using Compta.Core.Classes;
using Compta.Core.Structures;
using Compta.Core;

namespace ComptaPerso
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		//SQLiteFacilites sf;
		
		//public BaseManager core;
		
		public ComptaCore core;
		
		
		//UC_Home home;
		UC_BaseSelector bsel;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();	
			
			core = new ComptaCore();
			bsel = new UC_BaseSelector(this);
			InitForm();
			
		}
		
		void InitForm()
		{
			ClearPanel2Controls();
			
			// 
			TreeNav.Nodes.Clear();
			TreeNav.Nodes.Add("ACCOUNTS","Comptes");
			
			//
			AddPanel2Controls(bsel);
			
		}
		
		/// <summary>
		/// 
		/// </summary>
		public void ClearPanel2Controls()
		{
			splitContainer1.Panel2.Controls.Clear();
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="c"></param>
		public void AddPanel2Controls(Control c)
		{
			ClearPanel2Controls();
			c.Dock = DockStyle.Fill;
			this.splitContainer1.Panel2.Controls.Add(c);
		}
		
		
		/// <summary>
		/// Affiche un message "Section en chantier"
		/// </summary>
		public void EnChantier()
		{
			MessageBox.Show("Section encore en chantier...\n" +
			                "Les MAJ arriveront rapidement","Information",MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		
		/// <summary>
		/// 
		/// </summary>
		public void OpenDBWithDialog()
		{
			ofd = new OpenFileDialog();
			
			System.Windows.Forms.DialogResult dr = ofd.ShowDialog(); 
			if (dr == System.Windows.Forms.DialogResult.OK)
			{
				OpenDB(ofd.FileName.Trim());
				
			}			
		}
		

		
		public void OpenDB(string filename)
		{
			if (core.IsOpen)
			{
				DisconnectDB();
			}
			core.OpenDBCompta(filename);
			ShowTreeAccounts();
			this.Text = "Compta Perso - " + filename;
			
			if (core.IsOpen)
			{
				
				ActiveFunctions(true);
				
				bsel.AddFile(filename);
				bsel.SaveOpenedFile();
				
				// home
				UC_Home home = new UC_Home(this, core);
				AddPanel2Controls(home);
				
			}
			
		}
		
		/// <summary>
		/// Affichage des comptes dans le Treeview
		/// </summary>
		void ShowTreeAccounts()
		{
			TreeNav.Nodes["ACCOUNTS"].Nodes.Clear();
			foreach(Account a in core.ListAccounts)
			{
				TreeNav.Nodes["ACCOUNTS"].Nodes.Add(a.Id.ToString(), a.Name);
			}
			TreeNav.Nodes["ACCOUNTS"].Expand();
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="Acc"></param>
		public void AddAccountToTreeNav(Account Acc)
		{
			TreeNav.Nodes["ACCOUNTS"].Nodes.Add(Acc.Id.ToString(), Acc.Name);
		}
		
		
		/// <summary>
		/// Création d'un nouvelle base
		/// </summary>
		public string CreateNewBase()
		{
			string fname;
			sfd = new SaveFileDialog();
			
			System.Windows.Forms.DialogResult r = sfd.ShowDialog();
			if (System.IO.File.Exists(sfd.FileName))
			{
				System.IO.File.Delete(sfd.FileName);
			}
			
			if (r == System.Windows.Forms.DialogResult.OK)
			{
				fname = sfd.FileName.Trim();			
				if (System.IO.File.Exists(fname))
				{
					System.IO.File.Delete(fname);
				}
				
				core.CreateDBFile(fname);
			}
			else{
				fname = "";
			}
			return fname;
		}

		

		/// <summary>
		/// 
		/// </summary>
		public void DisconnectDB()
		{
			core.CloseCurrentDBFile();
			this.Text = "Compta Perso";
			if (!core.IsOpen)
			{
				ActiveFunctions(false);				
			}
			core.ClearLists();
			TreeNav.Nodes["ACCOUNTS"].Nodes.Clear();
			ClearPanel2Controls();
			
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="active"></param>
		void ActiveFunctions(bool active)
		{
			btnOpenDB.Enabled = !active;
			btnCloseDB.Enabled = active;
			
			btnAddAccount.Enabled = active;
			btnManageCategories.Enabled = active ;
			btnManageDenomination.Enabled = active;
			btnGoHome.Enabled = active;
			
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void TreeNavNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (TreeNav.SelectedNode.Level == 1 && TreeNav.SelectedNode.Parent.Name.Equals("ACCOUNTS"))
			{
				UC_Account compte = new UC_Account(this,Convert.ToInt32(TreeNav.SelectedNode.Name));
		    	AddPanel2Controls(compte);
			}
		}
		

		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void ToolStripMenuItem1Click(object sender, EventArgs e)
		{
			PopUp pop = new PopUp();
			UC_Operation op = new UC_Operation(core, new Account(), pop);
			
			pop.AddControl(op);
			
			pop.ShowDialog();		
			
		}
		
		void Panel2ControlRemoved(object sender, ControlEventArgs e)
		{
			// ajout de l'UC_Home (synthèse des comptes, des virements ...)
			
			
			
		}
		
		/// <summary>
		/// Affichage de la page d'accueil et des info synthétique sur les comptes
		/// </summary>
		void GoHome()
		{
			
		}
	}
}
